#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define fk cerr<<"fk"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
#define db2(x,y) cerr<<(#x)<<'='<<(x)<<' '<<(#y)<<'='<<(y)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
struct edge
{
    int u,v;
    i64 w;
    /*edge(int x,int y,i64 z)
        :u(x)
        ,v(y)
        ,w(z)
    {}*/
    bool operator<(const edge& other) const
    {
        return w<other.w;
    }
    bool operator>(const edge& other)const
    {
        return w>other.w;
    }
};
int f[200010];
int findroot(int x)
{
    return x==f[x]?x:f[x]=findroot(f[x]);
}
void Union(int x,int y)
{
    int fx=findroot(x);
    int fy=findroot(y);
    if(fx!=fy)
    {
        f[fx]=fy;
    }
}
void solve()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=200005;i++)
    {
        f[i]=i;
    }
    set<int> vis;
    priority_queue<edge,vector<edge>,greater<edge>> pq;
    rep(i,1,m)
    {
        int k,c;
        cin>>k>>c;
        vi t(k);
        rep(j,0,k-1)
        {
            cin>>t[j];
            vis.insert(t[j]);
        }
        for(int j=0;j<=k-2;j++)
        {
            pq.push({t[j],t[j+1],c});
        }
    }
    if(vis.size()!=n)
    {
        cout<<-1;
        return;
    }
    i64 ans=0;
    while(!pq.empty())
    {
        auto [u,v,w]=pq.top();
        pq.pop();
        if(findroot(u)==findroot(v))
        {
            continue;
        }
        ans+=w;
        Union(u,v);
    }
    cout<<ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout<<fixed;
    int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif
    
#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}
